package com.mrzhou.file.util;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;

public class DefineFontProvider extends XMLWorkerFontProvider {
    @Override
    public Font getFont(final String fontname, final String encoding, final boolean embedded, final float size,
                        final int style, final BaseColor color) {
        BaseFont bf = null;
        try {
//            URL url = new URL("https://osstest.minmaxtec.com/mmcloud/config/kaiti.ttf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%4012345678%2F20200721%2F%2Fs3%2Faws4_request&X-Amz-Date=20200721T013716Z&X-Amz-Expires=432000&X-Amz-SignedHeaders=host&X-Amz-Signature=7e7fc27e2254bc41f337522ee262e55a53aa8c47c9b6c6f217b82351fdd9cc25");
            InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("ttf/kaiti.ttf");
            byte[] bytes = IOUtils.toByteArray(is);
            bf = BaseFont.createFont("temp.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, true, bytes, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Font font = new Font(bf, size, style, color);
        font.setColor(color);
        return font;
    }
}
